package designPatterms.filter;

import java.util.ArrayList;
import java.util.List;

/**
 *    责任链模式
 * Created by yu756 on 2020/1/4 0:13
 */
public class Main {
    public static void main(String[] args) {
        Msg msg = new Msg();
        msg.setMsg("大家好:,<html> study hard , zhanchuanyu.com .暴力膜蛤</html>");

        FilterChain filterChain = new FilterChain();
        filterChain.add(new HtmlFilter()).add(new SensetiveFilter());


        FilterChain filterChain1 = new FilterChain();
        filterChain1.add(new URLFilter()).add(new FaceFilter());

        filterChain.add(filterChain1);
        filterChain.doFilter(msg);
        System.out.println(msg.getMsg());

    }

}

class Msg {
    String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

interface Filter {
    boolean doFilter(Msg m);
}

class HtmlFilter implements Filter {

    @Override
    public boolean doFilter(Msg m) {
        String r = m.getMsg();
        r = r.replace("<", "{").replace(">", "}");
        m.setMsg(r);
        return true;

    }
}

class URLFilter implements Filter {

    @Override
    public boolean doFilter(Msg m) {
        String r = m.getMsg();
        r = r.replace("zhanachuaanyu.com", "https://www.zhanchuanyu.com");
        m.setMsg(r);
        return true;

    }
}


class SensetiveFilter implements Filter {

    @Override
    public boolean doFilter(Msg m) {
        String r = m.getMsg();
        r = r.replace("暴力膜蛤", "谈笑风生");
        m.setMsg(r);
        return true;
    }
}

class FaceFilter implements Filter {

    @Override
    public boolean doFilter(Msg m) {
        String r = m.getMsg();
        r = r.replace(":)", "~V~");
        m.setMsg(r);
        return true;
    }
}

class FilterChain implements Filter {
    List<Filter> filters = new ArrayList<>();

    public FilterChain add(Filter filter) {

        filters.add(filter);
        return this;

    }

    @Override
    public boolean doFilter(Msg m) {

        for (Filter filter : filters) {
            if (!filter.doFilter(m)) return false;
        }
        return true;
    }
}


